import api from '../../api/api'
import {addressList, addressdefault, addressDel} from '../../api/conf'

Page({
  data: {
    addressList: [],
    deleteID: '',
    lastDefaultIndex: null,
    showDeleteConfirm: false,
    selectMode: false
  },
  onLoad: function(options) {
    const { selectMode = ''} = options;
    this.setData({
      selectMode: !!selectMode,
    });
    this.getAddressList();
  },
  getAddressList: function () {
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wx.showNavigationBarLoading()
    const { id } = this.data;
    api.get(addressList).then((res) => {
      res.Data.forEach((address,index) => {
        if (address.Id === id) {
          address.checked = true;
        }
        address.AddressDetail = address.ProvinceName + address.CityName + address.AreaName + address.Address
        address.defaultText = address.Default?"已设为默认":"设为默认"
        address.isDefault = address.Default
        if(address.isDefault){
          this.setData({
            lastDefaultIndex: index
          })
        }
      });
      this.setData({
        addressList: res.Data
      })
      wx.hideLoading()
      wx.hideNavigationBarLoading()
    })
  },
  createHandle: function (){
    if(this.data.addressList.length >= 1){
      wx.showToast({
        title: '最多添加20个',
        icon: 'error',
        duration: 2000
      })
      return ;
    }
    wx.navigateTo({ url: '/pages/address/address-edit/address-edit' });
  },
  deleteHandle: function (e){
    let that = this
    wx.showModal({
      title: '提示',
      content: '确定要删除吗？',
      success: function(sm) {
        if (sm.confirm) {
          wx.showLoading({
            title: '加载中',
            mask: true
          })
          const {index} = e.detail
          const {id} = e.detail
          let addressList = that.data.addressList
          addressList.splice(index,1)
          that.setData({addressList, lastDefaultIndex: index})
          api.post(addressDel + '?addressId=' + id, {}).then((res) => {
            wx.hideLoading()
          })
        }
      }
    })
  },
  editHandle: function (e){
    let obj = JSON.stringify(e.detail)
    wx.navigateTo({
      url: '/pages/address/address-edit/address-edit?item=' + obj
    });
  },

  selectHandle: function ({ detail }){
    if (this.data.selectMode) {
      let pages = getCurrentPages();
      let prevPage = pages[pages.length -2]
      prevPage.setData({
        addressId: detail
      })
      wx.navigateBack({ backRefresh: true });
    }
  },

  setDefaultHandle: function (e){
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    const {index} = e.detail
    const {id} = e.detail
    let addressList = this.data.addressList
    let lastDefaultIndex = this.data.lastDefaultIndex

    if(lastDefaultIndex !== null && lastDefaultIndex !== index ){
      addressList[lastDefaultIndex].isDefault = !addressList[lastDefaultIndex].isDefault
      addressList[lastDefaultIndex].defaultText = addressList[lastDefaultIndex].isDefault?"已设为默认":"设为默认"
    }

    addressList[index].isDefault = !addressList[index].isDefault
    addressList[index].defaultText = addressList[index].isDefault?"已设为默认":"设为默认"

    this.setData({addressList, lastDefaultIndex: index})
    api.post(addressdefault + '?addressId=' + id, {}).then((res) => {
      wx.hideLoading()
    })
  },
  getWxAddress: function () {
    wx.chooseAddress({
      success (res) {
        const addressData = {
          Id: 0,
          Name: res.userName,
          Phone: res.telNumber,
          Address: res.detailInfo,
          ProvinceName: res.provinceName,
          CityName: res.cityName,
          AreaName: res.countyName,
        }
        const obj = JSON.stringify(addressData)
        wx.navigateTo({
          url: '/pages/address/address-edit/address-edit?item=' + obj
        });
      },
      fail( err){
        console.warn('未选择微信收货地址', err);
      }
    })
  },
})